//#include "stdafx.h"
#include "stdafx.h"

#include "CImageControl.h"
#include "CViewImageControl.h"

#include "ViewYourImageView.h"
#include <GdiPlus.h>
#include "common_def.h"

//#include "ViewYourImageView.h"
//Macro to release COM Components

#ifdef SAFE_RELEASE
#undef SAFE_RELEASE
#endif
#define SAFE_RELEASE(x) do{\
	if((x) != NULL)\
{\
	while((x)->Release() != 0);\
	(x) = NULL;\
}\
}while(0)

//#include "global.h"
using namespace Gdiplus;

extern CString g_strBigImageName;

BEGIN_MESSAGE_MAP(CImageControl, CStatic)
	ON_WM_LBUTTONDOWN()
    ON_WM_MOUSEMOVE()
    
END_MESSAGE_MAP()

CImageControl::CImageControl(void)
{
	GdiplusStartupInput gdiplusStartupInput;
	GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
	m_pStream		= NULL;
	m_bIsPicLoaded	= FALSE;

	m_strImageName	= L"";
	m_nImageHeight	= 0;		// Image Height
	m_nImageWidth	= 0;		// Image Width
	m_nID			= 0;		// ID of control
    m_bSelected		= false;	// Image is non-selected
    m_bHover		= false;
    m_pCWndParaent	= NULL;
	m_bMouseMoveOnWnd = false;
}

void CImageControl::SetImageHeight( int ImageHeight )
{
	m_nImageHeight = ImageHeight;
}
void CImageControl::SetImageWidth( int ImageWidth )
{
	m_nImageWidth = ImageWidth;
}
int  CImageControl::GetImageHeight()
{
	return m_nImageHeight;
}
int  CImageControl::GetImageWidth()
{
	return m_nImageWidth;
}

CImageControl::~CImageControl(void)
{
	GdiplusShutdown(m_gdiplusToken);
}

BOOL CImageControl::CreateImageFromFile(CString &szFilePath)
{
	////Set success error state
	//SetLastError(ERROR_SUCCESS);
	//FreeData();

	//Allocate stream
	DWORD dwResult = CreateStreamOnHGlobal(NULL, TRUE, &m_pStream);
	if(dwResult != S_OK)
	{
		SetLastError(dwResult);
		return FALSE;
	}

	//Open the specified file
	CFile cFile;
	CFileException cFileException;
	if(!cFile.Open(szFilePath, CStdioFile::modeRead | CStdioFile::typeBinary, &cFileException))
	{
		SetLastError(cFileException.m_lOsError);
		SAFE_RELEASE(m_pStream);
		return FALSE;
	}
	// Save the path name
	m_strImageName = szFilePath;

	//Copy the specified file's content to the stream
	BYTE pBuffer[1024] = {0};
	while(UINT dwRead = cFile.Read(pBuffer, 1024))
	{
		dwResult = m_pStream->Write(pBuffer, dwRead, NULL);
		if(dwResult != S_OK)
		{
			SetLastError(dwResult);
			SAFE_RELEASE(m_pStream);
			cFile.Close();
			return FALSE;
		}
	}

	//Close the file
	cFile.Close();

	//Mark as Loaded
	m_bIsPicLoaded = TRUE;

//	Invalidate();
	RedrawWindow();

	return TRUE;
}

void CImageControl::PreSubclassWindow()
{
	CStatic::PreSubclassWindow();
	ModifyStyle(0, SS_OWNERDRAW);
}
void CImageControl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	//Check if pic data is loaded
	if(m_bIsPicLoaded)
	{

		//Get control measures
		RECT rc;
		this->GetClientRect(&rc);

		Graphics graphics(lpDrawItemStruct->hDC);
		Image image(m_pStream);
	//	graphics.DrawImage(&image, (INT)rc.left, (INT)rc.top, (INT)(rc.right-rc.left), (INT)(rc.bottom-rc.top));
        graphics.DrawImage(&image, ((INT)rc.right - this->m_nImageWidth) / 2, ((INT)rc.bottom - this->m_nImageHeight) / 2, (INT)this->m_nImageWidth, (INT)this->m_nImageHeight);

		 Pen* gdiPen = new Pen(COLOR_IMAGE_TOP_BORDER);
	//	 SolidBrush ColorBrush(	Color::LightBlue );
		 GraphicsPath path;

		 Gdiplus::Rect rect;
		 rect.X = rc.left;
		 rect.Y = rc.bottom - 8;
		 rect.Width = rc.right - rc.left - 1;
		 rect.Height = rc.bottom - rc.top;

		 path.AddRectangle(rect);
		 PathGradientBrush pthGrBrush(&path);
	
        if (this->m_bSelected == false && this->m_bHover == false)
        {
			//gdiPen->SetColor( Color ::Blue );
			gdiPen->SetColor( Color(255,0xFF, 0x00, 0xFF) );
		//	ColorBrush.SetColor( Color ::Blue );
			pthGrBrush.SetCenterColor(Color(255,0xFF, 0x00, 0xFF));
			Color colors[] = {Color(255, 0xFF, 0x00, 0xFF)};
			int count = 2;
			pthGrBrush.SetSurroundColors(colors, &count);
			//	 Color colors[] = {Color(255, 0, 255, 255)};
			graphics.FillRectangle(&pthGrBrush,  rect);

			
        }
        else //if (this->m_bHover == true)
        {
			
			//gdiPen->SetColor( Color ::LightBlue );
			gdiPen->SetColor( Color(255,0xFF, 0x0C, 0x00) );

			// Set the color at the center of the path
			pthGrBrush.SetCenterColor(Color(255,0xFF, 0x0C, 0x00));
			Color colors[] = {Color(255, 0xFF, 0x0C, 0x00)};
			int count = 2;

			// Set the color along the entire boundary of the path.
			pthGrBrush.SetSurroundColors(colors, &count);
			//	 Color colors[] = {Color(255, 0, 255, 255)};
			graphics.FillRectangle(&pthGrBrush,  rect);

        }
 
		graphics.DrawRectangle(gdiPen, (INT)rc.left, (INT)rc.top, (INT)rc.right - 2, (INT)rc.bottom - 8);

		graphics.FillRectangle(&pthGrBrush,  rect);

        delete gdiPen;
    }
}

void CImageControl::DrawRectangleSolid()
{
}

BOOL CImageControl::OnEraseBkgnd(CDC *pDC)
{
	if(m_bIsPicLoaded)
	{

		//Get control measures
		RECT rc;
		this->GetClientRect(&rc);

		Graphics graphics(pDC->GetSafeHdc());
		LARGE_INTEGER liSeekPos;
		liSeekPos.QuadPart = 0;
		m_pStream->Seek(liSeekPos, STREAM_SEEK_SET, NULL);
		Image image(m_pStream);
		//graphics.DrawImage(&image, (INT)rc.left, (INT)rc.top, (INT)(rc.right-rc.left), (INT)(rc.bottom-rc.top));
		graphics.DrawImage(&image, (INT)rc.left, (INT)rc.top, (INT)this->m_nImageWidth, (INT)this->m_nImageHeight);

		return TRUE;
	}
	else
	{
		return CStatic::OnEraseBkgnd(pDC);
	}
}


void CImageControl::OnLButtonDown(UINT nFlags, CPoint point)
{
	CWnd *hpParent = NULL;
	hpParent = GetParent();
	g_strBigImageName = m_strImageName;
   
	if ( hpParent != NULL )
	{
		CWnd *hpParentParent = NULL;
		hpParentParent = hpParent->GetParent();

		if ( hpParentParent != NULL )
		{
			CViewYourImageView *pViewYourImageView = (CViewYourImageView*)hpParentParent;
			pViewYourImageView->ShowBigPicture();
		}
		// set current index when the control is selected
	
		((CViewImageControl*)hpParent)->RefeshOldImageControl( ((CViewImageControl*)hpParent)->m_nIndexCurrent );
		((CViewImageControl*)hpParent)->m_nIndexCurrent = this->GetIndex();
	}
	this->m_bSelected = true;
}

void CImageControl::OnMouseMove(UINT nFlags, CPoint point)
{
	SetCapture();               //  Capture the mouse input
	CRect wndRect;
//  GetWndRect(&wndRect);
	GetWindowRect(&wndRect);
	ScreenToClient(&wndRect);

	// if x,y is in client rect
	if (wndRect.PtInRect(point))	
	{  
		if (this->m_bHover == false)
		{
			this->Invalidate( 1 );
			this->m_bHover = true;
		}
	} 
	else  // x,y is out client rect
	{
		ReleaseCapture();
		this->m_bHover = false;
		this->Invalidate( 1 );
	}
 
}

void CImageControl::SetSelectedValue(bool bSelected)
{
    this->m_bSelected = bSelected;
}

bool CImageControl::GetSelectedValue()
{
    return this->m_bSelected;
}

void CImageControl::SetHoverValue(bool bHover)
{
    this->m_bHover = bHover;
}

bool CImageControl::GetHoverValue()
{
    return this->m_bHover;
}

CString CImageControl::GetImageName()
{
    return this->m_strImageName;
}

void CImageControl::SetIndex(int nIndex)
{
    this->m_nIndex  = nIndex;
}
int CImageControl::GetIndex()
{
    return this->m_nIndex;
}